<?php


/**
 * 伪变量方式
 *
 * @param unknown_type $name
 * @return unknown
 */
function jllvar($name){
	
	return new cls_lasting_var($name);
		
}


/**
 * 全局持久化变量类
 * @author z.En Wong
 * @version 1.0.1
 * @date 08/21/2009
 */
class JL_lasting_var implements  ArrayAccess{
	
	private $value_arr = null;
	private $value = null;
	private $name = null;
	private $is_lock = null;
	
	
	/**
	 * 获得真实变量值
	 *
	 * @return unknown
	 */
	private function _get_value($name){
		
		return @unserialize(file_get_contents(LASTING_VAR_ROOT.'/'.$name));
	}
	
	
	/**
	 *
	 * 私有的保存文件方法
	 *	
	 *
	 */
	private function _save_value($name,$value_arr){

		file_put_contents(LASTING_VAR_ROOT.'/'.$name,serialize($value_arr));

	}
	
	/**
	 * 锁
	 *
	 */
	private function _lock($state){
		
		$value_arr = $this->_get_value($this->name);
		$value_arr[1] = $state;
		$this->_save_value($this->name,$value_arr);
		
		$this->is_lock = $state;
		
	}

	
	/**
	 * 得到锁定状态
	 *
	 * @return unknown
	 */
	private function _is_lock(){
		
		$value_arr = $this->_get_value($this->name);//获得真实变量值
		if (!isset($value_arr[1]) || !is_numeric($value_arr[1])) {//判断并且更改状态
			$value_arr[1] = 0;
		}
		$this->is_lock = $value_arr[1];//同步变量锁定状态到类中
		
		return $this->is_lock;
	}
	
	
	
	
	
	
	/**
	 * 构造方法
	 *
	 * @param unknown_type $name
	 */
	public function __construct($name){
		$this->JL_lasting_var($name);
		
		
	}
	
	/**
	 * 兼容的构造方法
	 *
	 * @param unknown_type $name
	 * @return cls_lasting_var
	 */
	public function JL_lasting_var($name){
		
		$this->name = $name;

		if (isset($GLOBALS['JL_lasting_var_obj_list'][$name])) {//如果变量已经实例化
			
			$value_arr = $GLOBALS['JL_lasting_var_obj_list'][$name]->get_arr();//获得变量的ARRAY形式
		}else {
			
			$value_arr = $this->_get_value($this->name);//获得变量数据
			
			if (!isset($value_arr[1]) || !is_numeric($value_arr[1])) {//判断并且更改状态
				
				$value_arr[1] = 0;
			}
		}
		
		$this->value_arr = $value_arr;//同步到类中

		$this->value = &$this->value_arr[0];
		$this->is_lock = &$value_arr[1];
		
		$GLOBALS['JL_lasting_var_obj_list'][$name] = &$this;
	}
	
	public function __destruct(){
		
		if ($this->is_lock()!==0) {//解锁
			$this->un_lock();
		}
		
		
	}
	
	
	
	
	
	public function get_arr(){
		return $this->value_arr;
	}
	
	/**
	 * 得到值
	 *
	 * @return unknown
	 */
	public function get(){
		
		
		return $this->value;
	}
	
	/**
	 * 设置值
	 *
	 * @param unknown_type $value
	 */
	public function set($value){
		$this->value = $value;
	}
	
	
	/**
	 * 保存值
	 *
	 * @param unknown_type $value
	 */
	public function save(){
		
		
		$value_arr_tmp = $this->_get_value($this->name);//获得真实变量数据
		
		if ($value_arr_tmp!=false && isset($value_arr_tmp[1]) && is_numeric($value_arr_tmp[1])) {//判断真实数据是否存在

			$this->is_lock = $value_arr_tmp[1];//同步锁定状态,如果存在就同步状态
		}
		
		if (empty($value_arr_tmp[1])) {//如果未锁定
			
			$this->_save_value($this->name,$this->value_arr);
		}else{
			
			throw new Exception('Value is locked');
		}
		
		
	}
	
	/**
	 * 销毁变量
	 *
	 */
	public function un_set(){
		
		@unlink(LASTING_VAR_ROOT.'/'.$this->name);
		unset($this->name);
		unset($this->value_arr);
		unset($this->value);
		unset($this->is_lock);
//		unset($GLOBALS['JL_factory_obj_list'][$this->name]);
	}
	
	/**
	 * 判断变量是否存在
	 *
	 */
	public function is_set(){
		
		return file_exists(LASTING_VAR_ROOT.'/'.$this->name);
	}
	
	/**
	 * 判断是否为空
	 *
	 * @return unknown
	 */
	public function is_empty(){
		return empty($this->value);
	}
	
	/**
	 * 判断变量是否为null
	 *
	 * @return unknown
	 */
	public function is_null(){
		return is_null($this->value);
	}
	
	
	
	public function lock(){
		$this->_lock(1);
	}
	
	public function un_lock(){
		$this->_lock(0);
	}
	
	public function is_lock(){
		return $this->_is_lock();
	}
	
	/********* ArrayAccess ***********/
	public function offsetGet($name){
		if (isset($this->value[$name])) {
			return $this->value[$name];
		}
		
	}
	public function offsetSet($name,$value){
		$this->value[$name] = $value;
		
	}
	public function offsetUnset($name){
		if (isset($this->value[$name])) {
			unset($this->value[$name]);
		}
		
	}
	public function offsetExists($offset){
		return isset($this->value[$offset]);
	}
	
	/********* ArrayAccess END ***********/
	
	
	
}

//===========================================







?>